package com.android.yinghebike.dialog;

import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;

import com.android.baselibrary.dialog.BaseDialogFragment;
import com.android.baselibrary.util.Util;
import com.android.yinghebike.R;
import com.android.yinghebike.constants.URLConstants;
import com.android.yinghebike.ui.activity.WebViewActivity;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/29.
 */

public class XieYi_DialogFragment extends BaseDialogFragment {

    @BindView(R.id.tv_text)
    TextView tv_text;
    @BindView(R.id.tv_cancle)
    TextView tv_cancle;

    boolean isSecond;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_xieyi;
    }

    @Override
    protected void initData() {
        if (isSecond) {
            tv_cancle.setText("不同意退出");
        }
    }

    @Override
    protected void initView() {
        String appName = getString(R.string.app_name);
        String text = "欢迎使用"+appName+"！在您使用"+ appName +"前，请认真阅读《用户协议》以及《隐私政策》，您同意并接受全部条款后方可开始使用"+ appName+"的产品与服务。";
        tv_text.setText(getTextSpan(text));
        tv_text.setMovementMethod(LinkMovementMethod.getInstance());
        tv_text.setHighlightColor(Color.TRANSPARENT);    //去掉点击高亮背景
    }

    @Override
    protected boolean setCanceledOnTouchBACK() {
        return false;
    }

    @Override
    protected int getShowDirection() {
        return BaseDialogFragment.Direction_FadeIn;
    }

    @Override
    public void setData(Map<String, Object> params) {
        if (params != null) {
            isSecond = (boolean) params.get("isSecond");

        }
    }

    public static BaseDialogFragment showDialog(FragmentManager fragmentManager, Map<String, Object> params) {
        XieYi_DialogFragment dialogFragment = new XieYi_DialogFragment();
        dialogFragment.setData(params);
        dialogFragment.show(fragmentManager, "");
        return dialogFragment;
    }


    @OnClick({R.id.tv_cancle, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancle:          //
                if ("不同意".equals(tv_cancle.getText().toString())) {
                    if (callBack != null) {
                        callBack.onResult(false);
                    }
                    dismiss();
                } else if ("不同意退出".equals(tv_cancle.getText().toString())) {
                    Util.closeApp();
                }
                break;
            case R.id.tv_ok:    //同意
                if (callBack != null) {
                    callBack.onResult(true);
                }
                break;
        }
    }

    public SpannableStringBuilder getTextSpan(String content) {
        SpannableStringBuilder ssb = new SpannableStringBuilder(content);

        ClickableSpan clickableSpan1 = new ClickableSpan() {
            public void onClick(View view) {
                //点击超链接时调用
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", URLConstants.URL_xieyi1);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);//当传入true时超链接下会有一条下划线
            }
        };
        int start1 = content.indexOf("《");
        int end1 = content.indexOf("》") + 1;
        //设置超链接文本的颜色
        ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#07C160")), start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ssb.setSpan(clickableSpan1, start1, end1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        ClickableSpan clickableSpan2 = new ClickableSpan() {
            public void onClick(View view) {
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", URLConstants.URL_xieyi2);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);//当传入true时超链接下会有一条下划线
            }
        };
        int start2 = content.lastIndexOf("《");
        int end2 = content.lastIndexOf("》") + 1;
        //设置超链接文本的颜色
        ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#07C160")), start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ssb.setSpan(clickableSpan2, start2, end2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return ssb;
    }


}
